package com.xiaoshuidi.cloud.module.contract.controller.admin.contract;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.signingSubject.*;
import com.xiaoshuidi.cloud.module.contract.convert.contract.SigningSubjectConvert;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.enums.contract.ContractTemplateTypeEnum;
import com.xiaoshuidi.cloud.module.contract.service.contract.SigningSubjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 签约主体")
@RestController
@RequestMapping(ApiConstants.NAME +"/signingSubject")
@Validated
public class ContractSubjectController {
    @Resource
    private SigningSubjectService signingSubjectService;

    @PostMapping("/create")
    @Operation(summary = "创建合同签约主体")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<Long> createSigningSubject(@Valid @RequestBody SigningSubjectCreateReqVO createReqVO) {
        return success(signingSubjectService.createSigningSubject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新合同签约主体")
//    @PreAuthorize("@ss.hasPermission('contract:subject:update')")
    public CommonResult<Boolean> updateSigningSubject(@Valid @RequestBody SigningSubjectUpdateReqVO updateReqVO) {
        signingSubjectService.updateSigningSubject(updateReqVO);
        return success(true);
    }

    @PutMapping("/switch")
    @Operation(summary = "线上签约开关")
//    @PreAuthorize("@ss.hasPermission('contract:subject:update')")
    public CommonResult<Boolean> signingSubjectSwitch(@Valid @RequestBody SigningSubjectSwitchReqVO updateReqVO) {
        SigningSubjectUpdateReqVO signingSubjectUpdateReqVO = SigningSubjectConvert.INSTANCE.convertSwitchVO2UpdateReqVO(updateReqVO);
        signingSubjectService.updateSigningSubject(signingSubjectUpdateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得合同签约主体")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('contract:subject:query')")
    public CommonResult<SigningSubjectRespVO> getSigningSubject(@RequestParam("id") Long id) {
        SigningSubjectRespVO respVO = signingSubjectService.getSigningSubject(id);
        return success(respVO);
    }

    @GetMapping("/list")
    @Operation(summary = "查询签约主体列表")
//    @PreAuthorize("@ss.hasPermission('contract:subject:query')")
    public CommonResult<List<SigningSubjectRespVO>> getList() {
        List<SigningSubjectRespVO> list = signingSubjectService.getSigningSubjectList();
        return success(list);
    }

    @PostMapping("/refApartment")
    @Operation(summary = "主体配置关联门店")
//    @PreAuthorize("@ss.hasPermission('contract:subject:ref-apartment')")
    public CommonResult<Boolean> refApartment(@Valid @RequestBody SigningSubjectRefApartmentReqVO reqVO) {
        signingSubjectService.relationApartment(reqVO);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得合同签约主体分页")
//    @PreAuthorize("@ss.hasPermission('contract:subject:query')")
    public CommonResult<PageResult<SigningSubjectRespVO>> getSigningSubjectPage(@Valid SigningSubjectPageReqVO pageVO) {
        PageResult<SigningSubjectRespVO> pageResult = signingSubjectService.getSigningSubjectPage(pageVO);
        return success(pageResult);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除合同签约主体")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('contract:subject:delete')")
    public CommonResult<Boolean> deleteSigningSubject(@RequestParam("id") Long id) {
        signingSubjectService.deleteSigningSubject(id);
        return success(true);
    }

    @GetMapping("/getRefApartmentList")
    @Operation(summary = "查询非当前主体下已关联主体的门店")
//    @PreAuthorize("@ss.hasPermission('contract:subject:query')")
    public CommonResult<List<Long>> getRefApartment(@RequestParam("id") Long id) {
        return success(signingSubjectService.getRefApartmentList(id));
    }


    @PostMapping("/get-verify-url")
    @Operation(summary = "创建合同签约主体获取实名认证地址")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<String> createSubjectGetUrl(@Valid @RequestBody SigningSubjectCreateReqVO createReqVO) {
        return success(signingSubjectService.createSubjectGetUrl(createReqVO));
    }

    @PostMapping("/add_signature")
    @Operation(summary = "签约主体印章上传")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<Boolean> addSubjectSignature(@Valid @RequestBody SigningSubjectAddSignatureReqVO reqVO) {
        signingSubjectService.addSubjectSignature(reqVO);
        return success(true);
    }

    @GetMapping("/auto-sign")
    @Operation(summary = "签约主体获取自动签约地址")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<String> getAutoSignUrl(@RequestParam("id") Long id) {
        return success(signingSubjectService.getAutoSignUrl(id));
    }

    @GetMapping("/sync-auto-sign")
    @Operation(summary = "同步自动签约数据")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<Boolean> syncAutoSign(@RequestParam("id") Long id) {
        signingSubjectService.syncAutoSign(id);
        return success(true);
    }

    @GetMapping("/online-sign-check")
    @Operation(summary = "线上签约判断")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<Boolean> onlineSignCheck(@RequestParam("roomId") Long roomId) {
        signingSubjectService.onlineSignCheck(roomId, ContractTemplateTypeEnum.RENT_CONTRACT.getCode());
        return success(true);
    }

    /**
     * 业主合同线上签约判断
     */
    @GetMapping("/owner-online-check")
    @Operation(summary = "业主合同线上签约判断")
//    @PreAuthorize("@ss.hasPermission('contract:subject:create')")
    public CommonResult<Boolean> ownerOnlineCheck(@RequestParam("roomId") Long roomId) {
        signingSubjectService.onlineSignCheck(roomId, ContractTemplateTypeEnum.OWNER_HOUSE_CONTRACT.getCode());
        return success(true);
    }

}
